<template>
  <el-card class="application-list-card">
    <h2>我的投递记录</h2>
    <el-form :inline="true" :model="searchForm" class="search-form" @submit.prevent>
      <el-form-item label="状态">
        <el-select v-model="searchForm.status" clearable placeholder="全部">
          <el-option label="待处理" :value="0" />
          <el-option label="已拒绝" :value="1" />
          <el-option label="已邀请面试" :value="2" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="fetchApplications">筛选</el-button>
        <el-button @click="resetSearch">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="applications" stripe border class="application-table" v-loading="loading">
      <el-table-column prop="positionName" label="岗位名称" min-width="120" />
      <el-table-column prop="enterpriseName" label="企业名称" min-width="120" />
      <el-table-column prop="applyTime" label="投递时间" min-width="140" />
      <el-table-column prop="status" label="状态" min-width="100">
        <template #default="{ row }">
          <el-tag :type="statusTagType(row.status)">{{ statusMap[row.status] || row.status }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="remark" label="备注" min-width="120" />
    </el-table>
    <div class="pagination-bar">
      <el-pagination
        background
        layout="prev, pager, next, jumper"
        :total="total"
        :page-size="searchForm.size"
        :current-page="searchForm.page"
        @current-change="onPageChange"
      />
    </div>
  </el-card>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import axios from '@/utils/axios'

const searchForm = ref({ status: null, page: 1, size: 10 })
const applications = ref([])
const allApplications = ref([])
const total = ref(0)
const loading = ref(false)

const statusMap = {
  PENDING: '待处理',
  REJECTED: '已拒绝',
  INTERVIEW_INVITED: '已邀请面试'
}
const statusTagType = status => {
  if (status === 'PENDING') return 'info'
  if (status === 'REJECTED') return 'danger'
  if (status === 'INTERVIEW_INVITED') return 'success'
  return ''
}
const fetchApplications = async () => {
  loading.value = true
  try {
    const params = { ...searchForm.value }
    if (params.status === null || params.status === '') delete params.status
    // 不传分页参数，获取全部
    delete params.page
    delete params.size
    const res = await axios.get('/applications/student', { params })
    const data = res.data || res
    allApplications.value = data.list || []
    total.value = allApplications.value.length
    // 默认显示第一页
    const start = (searchForm.value.page - 1) * searchForm.value.size
    const end = start + searchForm.value.size
    applications.value = allApplications.value.slice(start, end)
  } catch (e) {}
  loading.value = false
}
const resetSearch = () => {
  searchForm.value = { status: null, page: 1, size: 10 }
  fetchApplications()
}
const onPageChange = page => {
  searchForm.value.page = page
  // 只切片，不重新请求
  const start = (searchForm.value.page - 1) * searchForm.value.size
  const end = start + searchForm.value.size
  applications.value = allApplications.value.slice(start, end)
}
onMounted(() => {
  fetchApplications()
})
</script>

<style scoped>
.application-list-card {
  max-width: 1100px;
  margin: 40px auto;
  padding: 30px 20px;
}
.search-form {
  margin-bottom: 20px;
}
.application-table {
  margin-bottom: 20px;
}
.pagination-bar {
  text-align: right;
  margin-top: 10px;
}
</style> 